package testing.main;

public class YAMLSerialiser {
   /* int questionKey;
    int testKey;
    Map<Question,Integer> questionMap;
    Map<Test,Integer> testMap;

    public YAMLSerialiser() {
        this.questionMap = new LinkedHashMap<Question, Integer>();
        this.testMap = new LinkedHashMap<Test, Integer>();
        questionKey = 0;
        testKey = 0;
    }

    public TestSys deserialiseAll(String text) throws FileProblemException {

        if(text == null)
        throw new FileProblemException("Where is non null File?!");
        HashMap<Integer, Question> idQuestionMap = new HashMap<Integer, Question>();
        HashMap<Integer,Test> idTestMap = new HashMap<Integer, Test>();
        Pattern questionPattern = Pattern.compile("!testing.domain.question.Question\\s*(\\d+?)\\s*name:([^\n]*)\n" +
                "rightAnswer:([^\n]*)\\s*answers:\\[(.*)\\]\\s*");
        Matcher matcher = questionPattern.matcher(text);
        while (matcher.find())
        {
            int num = Integer.valueOf(matcher.group(1));
            System.out.println("The num of question : "+num);
            String name = matcher.group(2);
            System.out.println(name);
            String rightAnswer = matcher.group(3);
            System.out.println("Answer is : "+rightAnswer);
            String[] answers = matcher.group(4).trim().split(", ");
            for (int i=0;i<answers.length;i++)
            {
                System.out.print(answers[i]+" | ");
            }
            System.out.println("\n************************");
            Question ques = new Question(num,name,rightAnswer,answers);
            idQuestionMap.put(num,ques);
        }

        Pattern testPattern = Pattern.compile("!testing.domain.test.Test\\s*(\\d+?)\\s*name:([^\n]*)\\s*");
        matcher = testPattern.matcher(text);
        while (matcher.find())
        {
            int num = Integer.valueOf(matcher.group(1));
            System.out.println("The num of test : "+num);
            String name = matcher.group(2);
            System.out.println(name+"\n----------------------");
            Test test = new Test(name);
            idTestMap.put(num,test);
        }

        TestSys tSystem = new TestSys(new ArrayList<Question>(idQuestionMap.values()),new ArrayList<Test>(idTestMap.values()));
        return tSystem;
}
     private void serialize(Question ques,FileWriter writer) throws IOException {
        if(!questionMap.containsKey(ques))
        {
            String questionTemplate = String.format(
                    "\n\n---\n!testing.domain.question.Question %d\nname:%s\nrightAnswer:%s\nanswers:%s",
                    questionKey, ques.getName(), ques.getRightAnswer(), Arrays.toString(ques.getAnswers()));
            writer.write(questionTemplate);
            questionMap.put(ques,questionKey++);
        }


    }
     private void serialize(Test test,FileWriter writer) throws IOException {
        if(!testMap.containsKey(test))
        {
            String testTemplate = String.format(
                    "\n\n---\n!testing.domain.test.Test %d\nname:%s",
                    testKey, test.getName());
            writer.write(testTemplate);
            testMap.put(test,testKey++);
        }


    }


     public void serializeAll(TestSys tSystem,FileWriter writer) throws IOException {
        for(Question ques:tSystem.questions)
            serialize(ques,writer);
        for(Test test:tSystem.tests)
            serialize(test,writer);
    } */
}
